import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import common.SQLCall;

/**
 * Upload the template of document needed for an exam
 * 
 */
public class DocumentUpload
{
	public static void main(String[] args)
	{
		Connection conn = null;
		try
		{
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@146.187.134.17:1542:oracle10g",
					"cscd494", "R3tral435");

			if (SQLCall.isFound(conn, "document_id", "documents"))
				return;

			String[] names = new String[2];
			names[0] = "doc/exam_instructions.html";
			names[1] = "doc/jar_instructions.html";
			File[] file = new File[2];
			file[0] = new File(names[0]);
			file[1] = new File(names[1]);
			FileInputStream[] input = new FileInputStream[2];
			input[0] = new FileInputStream(file[0]);
			input[1] = new FileInputStream(file[1]);

			int documentID;

			PreparedStatement state = conn
					.prepareStatement("INSERT INTO DOCUMENTS (document_id, document_name, document_file) VALUES(?,?,?)");

			for (int i = 0; i < 2; i++)
			{
				documentID = SQLCall.getNextSeq(conn, "document_s1");

				state.setInt(1, documentID);
				state.setString(2, file[i].getName());
				state.setBinaryStream(3, input[i], (int) file[i].length());

				state.execute();

			}
			state.close();
			conn.close();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
